package com.example.service;

import com.example.dao.ProductsDao;
import com.example.entity.Products;
import com.example.entity.Params;
import com.example.exception.CustomException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ProductsService {

    @Resource
    private ProductsDao productsDao;

    public List<Products> findAll() {
        return productsDao.selectAll();
    }

    public PageInfo<Products> findBySearch(Params params) {
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<Products> list = productsDao.findBySearch(params);
        return PageInfo.of(list);
    }

    public void add(Products product) {
        if(product.getProductName() == null || product.getProductName().trim().equals("")) {
            throw new CustomException("商品名称不能为空");
        }
        productsDao.insertSelective(product);
    }

    public void update(Products product) {
        productsDao.updateByPrimaryKeySelective(product);
    }

    public void delete(Integer productId) {
        productsDao.deleteByPrimaryKey(productId);
    }

    public Products findById(Integer productId) {
        return productsDao.selectByPrimaryKey(productId);
    }
}
